package com.ybbase.framework.controller.flow.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybbase.framework.base.controller.BaseController;
import com.ybbase.framework.base.model.ResponseBean;
import com.ybbase.framework.common.constant.CommonResultCode;
import com.ybbase.framework.model.po.flow.FlowConfig;
import com.ybbase.framework.model.po.flow.NodeConfig;
import com.ybbase.framework.service.flow.FlowConfigService;
import com.ybbase.framework.service.flow.NodeConfigService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程业务配置Controller
 * @author cg
 * @date 2019-11-04 16:25
 * @since 4.0
 */
@RestController
@RequestMapping("/flowbusinessconfig")
public class FlowBusinessConfigController extends BaseController {

    private static final Logger logger = LogManager.getLogger(FlowBusinessConfigController.class);

    @Autowired
    FlowConfigService flowConfigService;
    @Autowired
    NodeConfigService nodeConfigService;

    /**
     * 查询业务配置列表
     * @author cg
     * @date 2019-09-06 15:09
     * @param flowConfig 业务配置信息
     * @return ResponseBean
     * @since 4.0
     */
    @GetMapping("/findBusinessConfigList")
    @ApiOperation(value = "查询业务配置列表", notes = "查询业务配置列表")
    @ApiImplicitParam(name = "businessConfig", value = "业务配置实体", dataType = "流程业务模型配置表")
    public ResponseBean findBusinessConfigList(FlowConfig flowConfig,@RequestParam(name = "page", defaultValue = "1") int pageIndex,
                                               @RequestParam(name = "rows", defaultValue = "20") int step){
        QueryWrapper<FlowConfig> wrapper = new QueryWrapper<>();
        if(flowConfig.getFlowCode()==""||"".equals(flowConfig.getFlowCode())){
            flowConfig.setFlowCode(null);
        }
        wrapper.lambda().eq(FlowConfig::getFlowCode, flowConfig.getFlowCode());
        Page<FlowConfig> page = new Page(pageIndex,step);
        try {
            flowConfigService.page(page,wrapper);
            if (page.getRecords()!=null&&!page.getRecords().isEmpty()){
                for(FlowConfig f:page.getRecords()){
                    QueryWrapper<NodeConfig> nodeConfigQueryWrapper = new QueryWrapper<>();
                    nodeConfigQueryWrapper.lambda().eq(NodeConfig::getFlowCode, f.getFlowCode());
                    List<NodeConfig> list = nodeConfigService.list(nodeConfigQueryWrapper);
                    if(list!=null&&!list.isEmpty()){
                        String nodeNames = "";
                        for (NodeConfig n:list){
                            nodeNames = nodeNames + n.getNodeName() +"|";
                        }
                        f.setNodeNames(nodeNames);
                    }
                }
            }
        }catch(Exception e){
            logger.error("查询业务配置列表",e.getMessage());
            e.printStackTrace();
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "查询业务配置列表失败", new Page());
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(), CommonResultCode.SUCCESS.getMsg(), page);
    }

    /**
     * 查询业务配置列表不带分页
     * @return ResponseBean
     * @author cg
     * @date 2019-11-11 16:13
     * @since 4.0
     */
    @GetMapping("/getBusinessList")
    @ApiOperation(value = "查询业务配置列表不带分页", notes = "查询业务配置列表不带分页")
    @ApiImplicitParam(name = "businessConfig", value = "业务配置实体", dataType = "流程业务模型配置表")
    public ResponseBean getBusinessList(){
        List<FlowConfig> bs = new ArrayList<>();
        try{
            QueryWrapper<FlowConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(FlowConfig::getIsEnable,1);
            bs = flowConfigService.list(queryWrapper);
        }catch (Exception e){
            logger.error("查询业务配置列表不带分页",e.getMessage());
            e.printStackTrace();
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"查询业务配置列表失败");
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(), CommonResultCode.SUCCESS.getMsg(), bs);
    }

    /**
     * 保存配置流程节点方法
     * @param flowConfig 业务配置信息
     * @return ResponseBean
     * @author cg
     * @date 2019-11-04 16:50
     * @since 4.0
     */
    @PostMapping("/saveConfigFlowNode")
    @ApiOperation(value = "保存配置流程节点方法", notes = "保存配置流程节点方法")
    @ApiImplicitParam(name = "flowConfig", value = "业务配置实体", dataType = "流程业务模型配置表")
    public ResponseBean saveConfigFlowNode(FlowConfig flowConfig){
        try {
            flowConfigService.save(flowConfig);
        }catch (Exception e){
            logger.error("保存配置流程节点方法",e.getMessage());
            e.printStackTrace();
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "查询业务配置列表失败", null);
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(), CommonResultCode.SUCCESS.getMsg(), null);
    }




}
